package com.Input;

import com.pi4j.io.serial.Serial;
import com.pi4j.io.serial.SerialDataEvent;
import com.pi4j.io.serial.SerialDataListener;
import com.pi4j.io.serial.SerialFactory;
import com.pi4j.io.serial.SerialPortException;

public class OrpInput implements Runnable {
	
	double orp = 0;
	
	public double getORP() {
		return orp;
	}
	
	@Override
	public void run() {
		
		System.out.println(" ... ORP THREAD STARTED ...");
		System.out.println(" ... COMMUNICATING VIA UART ...");
        
        // create an instance of the serial communications class
        final Serial serial = SerialFactory.createInstance();

        // create and register the serial data listener
        serial.addListener(new SerialDataListener() {
            @Override
            public void dataReceived(SerialDataEvent event) {
                // print out the data received to the console
                //System.out.println("ORP: " + event.getData());
                orp = Double.parseDouble(event.getData());
            }            
        });
                
        try {
            // open the default serial port provided on the GPIO header
            serial.open(Serial.DEFAULT_COM_PORT, 38400);    
            for(;;) {
            	Thread.sleep(500);
            }
        }
        catch(Exception e) {
            System.out.println(" ==>> UART SETUP FAILED : " + e.getMessage());
            return;
        }   
	}
    
}
